﻿--[[
   @brief    update_guide_view
   @author   neilzxhuang
   @version  1.0
   @date     2015-04-14
--]]

update_guide_tips_view =
{
    update_guide_exist = false;
    update_confirm_exist = false;
    opened_autoupdate_list = nil;  -- 保存已经点击过的button id
    main_wnd_rect = {};
    init = function()
        ierd_tgp.tpf_ui.Log("update_guide_view init.");
    end;

    -- 注意：后续请尽量使用通用tips组件 ui_utility.show_tips(param);
    show_dialog = function(show)
        update_guide_tips_control.get_main_wnd_rect();
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_AUTO_UPDATE_DLG);
        --component:Show(show);
        if show == TRUE then
            --component:Restore();
            -- ui_utility.do_fake_modal(CID_AUTO_UPDATE_DLG);
            local current_rect = component:GetRectEx(1,TRUE);
            local tpf_ui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            local sys_param = ierd_tgp.tpf_ui.SysParams:new();
            tpf_ui:get_sys_params(sys_param);
            local work_area_width = sys_param.work_area_width;
            local work_area_height = sys_param.work_area_height;
            sys_param:delete();
            -- component:MoveToCenter();
            -- 移动到右下角
            local rect_dlg = component:GetRectEx(TRUE,TRUE);
            rect_dlg.left = work_area_width - 320 - 5;
            rect_dlg.top = work_area_height - 330 - 5;
            rect_dlg.right = rect_dlg.left + 320;
            rect_dlg.bottom = rect_dlg.top + 330;
            component:MoveEx(rect_dlg,TRUE);
        else
            -- ui_utility.end_fake_modal(CID_AUTO_UPDATE_DLG);
            component = ui_utility.get_ctrl_ptr_by_cid(CID_AUTO_UPDATE_GAME_LIST);
            --component:ClearAllItems();
            update_guide_tips_view.opened_autoupdate_list = nil;
            update_guide_tips_control.clear_all_data_info();
            update_guide_tips_view.update_guide_exist = false;
            local control_id = ui_utility.get_ctrl_id_by_cid(CID_AUTO_UPDATE_DLG);
            layout_mgr.delay_remove_ctrl(control_id);

        end
    end;

    -- 关闭button响应函数
    cancel_click = function(uilib, dwID_Click)
        update_guide_tips_view.show_dialog(FALSE);
        -- 主动关闭弹框，上报数据
        tqos_mgr.remember_btn_clicked(update_guide_constant.UPDATE_GUIDE_CLOSE);
    end;

    show_item_info = function(base_info, control_id)
        local url = base_info.banner_icon_url;
        local name = base_info.name;
        ierd_tgp.tpf_ui.Log(string.format("set_little_icon, gamename=%s", name));
        -- 设置头像
        update_guide_tips_view.set_little_image(url, control_id);
        -- 设置名称
        update_guide_tips_view.set_game_name(name, control_id);
    end;

    set_little_image = function(url, control_id)
        -- little image show
        local pro_value = string.format("ONEIMAGE:client_tips\\def_square_img.png|%s;Stretch;null;", url);
        local little_image_id = update_guide_tips_view.get_child_control_id(control_id, "game_little_image");
        if little_image_id == Tenio.INVALID_CONTROLID then
            ierd_tgp.tpf_ui.Log("get little image id fail");
            return;
        end
        ui_utility.setproperty_by_id(little_image_id, 'Background', pro_value);
    end;

    -- 设置游戏名称
    set_game_name = function(name, control_id)
        local gamename_id = update_guide_tips_view.get_child_control_id(control_id, "game_name");
        if gamename_id == Tenio.INVALID_CONTROLID then
            ierd_tgp.tpf_ui.Log("get game name id fail");
            return;
        end
        ui_utility.setproperty_by_id(gamename_id, 'Caption', name);
    end;

    -- 设置开启的人数
    set_openuser_num = function(dw_num, dw_ctrl_id)
        local dw_usernum_id = update_guide_tips_view.get_child_control_id(
                                            dw_ctrl_id,
                                            "txtUserNum");
        if Tenio.INVALID_CONTROLID == dw_usernum_id then
            ierd_tgp.tpf_ui.Log("get autoupdate people num id fail");
            return;
        end;
        -- 计算人数
        local s_usernum = "";
        if base_utility.get_current_language() == 2 then   -- en-us
            if dw_num > 1000 then
                local dw_numtemp = dw_num / 1000;
                s_usernum = string.format("%.1f"..LoadStr("AUTO_UPDATE_GUIDE_K").." "..LoadStr("AUTO_UPDATE_GUIDE_USERS_OPENED"), dw_numtemp);
            else
                s_usernum = tostring(dw_num).." "..LoadStr("AUTO_UPDATE_GUIDE_USERS_OPENED");
            end
        else 
            if dw_num > 10000 then
                local dw_numtemp = dw_num / 10000;
                s_usernum = string.format("%.1f"..LoadStr("AUTO_UPDATE_GUIDE_W").." "..LoadStr("AUTO_UPDATE_GUIDE_USERS_OPENED"), dw_numtemp);
            else
                s_usernum = tostring(dw_num).." "..LoadStr("AUTO_UPDATE_GUIDE_USERS_OPENED");
            end
        end
        ui_utility.setproperty_by_id(dw_usernum_id, 'Caption', s_usernum);
    end;

    get_child_control_id = function(parent_id, child_name)
        local path_str,path_hoder = U8toCUIStr(child_name);
        local uilib = ui_utility.uilib();
        return uilib:GetControlIDbyPath(path_str, parent_id);
    end;

    click_game_update_box = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("click_game_update_box");
        update_guide_tips_view.close_confirm_click(uilib, dwID_Click);
        local param = {};
        param.type = "tools";
        param.sub_page = "AUTO_UPDATE";
        local json_str = base_utility.table_to_json(param);
        web_utility.open_link(json_str);
    end;

    -- “开启自动更新”按钮响应函数
    open_autopudate_click = function(uilib, dwID_Click)
        -- 判断是否是重复点击
        if nil ~= update_guide_tips_view.opened_autoupdate_list and 
                table.getn(update_guide_tips_view.opened_autoupdate_list) > 0 then
            bOpened = false;
            for i = 1, table.getn(update_guide_tips_view.opened_autoupdate_list) do
                if dwID_Click == update_guide_tips_view.opened_autoupdate_list[i] then
                    bOpened = true;
                    break;
                end;
            end;
            if bOpened then
                return;
            end;
        end;
        local param = {};
        for index, control_id in pairs(update_guide_tips_control.game_id_list) do
            local openbtn_id = update_guide_tips_view.get_child_control_id(control_id, "btnOpenAutoUpdate");
            if Tenio.INVALID_CONTROLID ~= openbtn_id then
                -- 查找当前点击的Btn
                if openbtn_id == dwID_Click then
                    -- 改变自更新设置
                    param.game_id = index;
                    param.auto_update = true;
                    game_update_control.change_auto_update_setting(param);
                    -- 改变btn状态
                    update_guide_tips_view.change_btn_state(dwID_Click);
                    -- 保存
                    if nil == update_guide_tips_view.opened_autoupdate_list then
                        update_guide_tips_view.opened_autoupdate_list = {};
                    end;
                    table.insert(update_guide_tips_view.opened_autoupdate_list, dwID_Click);

                    -- 上报数据
                    tqos_mgr.report_autoupdate_open(update_guide_constant.UPDATE_GUIDE_OPEN, index);
                    break;
                end;
            end;
        end
    end;

    -- 修改开启后的Btn的显示状态
    change_btn_state = function(dwID_Click)
        if Tenio.INVALID_CONTROLID == dwID_Click then
            ierd_tgp.tpf_ui.Log("change_btn_state, btn id is invalid");
            return;
        end;
        ui_utility.setproperty_by_id(dwID_Click, "Background",
            "ONEIMAGE:update_tips\\auto_update_tips.png?11,360,253,361;Stretch;null;");
        ui_utility.setproperty_by_id(dwID_Click, "BackgroundHover",
            "ONEIMAGE:update_tips\\auto_update_tips.png?11,360,253,361;Stretch;null;");
        ui_utility.setproperty_by_id(dwID_Click, "BackgroundPressed",
            "ONEIMAGE:update_tips\\auto_update_tips.png?11,360,253,361;Stretch;null;");
        -- 设置显示文本
        ui_utility.setproperty_by_id(dwID_Click, "Caption", LoadStr("AUTO_UPDATE_GUIDE_OPENED"));
    end;

    -- “去设置”按钮响应函数
    goset_click = function(uilib, dwID_Click)
        update_guide_tips_view.open_tools_autoupdate_link();
    end;

    -- 打开“工具箱--autoupdate page”
    open_tools_autoupdate_link = function()
        -- 获取第一个游戏的gameid
        local param = {};
        param.type = "tools";
        param.sub_page = "AUTO_UPDATE";
        if update_guide_tips_control.update_game_list == nil or table.getn(update_guide_tips_control.update_game_list) == 0 then
            param.game_id = "0";
        else
            param.game_id = tostring(update_guide_tips_control.update_game_list[1]);
        end;
        web_utility.open_link(base_utility.table_to_json(param));

        -- 关闭窗口显示
        update_guide_tips_view.show_dialog(FALSE);

        -- "去设置"按钮，上报数据
        tqos_mgr.remember_btn_clicked(update_guide_constant.UPDATE_GUIDE_MORE);
    end;

    -- 待开启游戏数量设置
    set_update_game_num = function(dw_num)
        local dw_dlg_id = ui_utility.get_ctrl_id_by_cid(CID_AUTO_UPDATE_DLG);
        if Tenio.INVALID_CONTROLID == dw_dlg_id then
            ierd_tgp.tpf_ui.Log("get update dlg id failed");
            return;
        end;
        local dw_gamenum_id = update_guide_tips_view.get_child_control_id(
                                                    dw_dlg_id,
                                                    "txtGameNum");
        if Tenio.INVALID_CONTROLID == dw_gamenum_id then
            ierd_tgp.tpf_ui.Log("get game num id failed");
            return;
        end;
        local s_gamenum_txt = LoadStr("AUTO_UPDATE_GUIDE_TOTAL").." "..tostring(dw_num).." "..LoadStr("AUTO_UPDATE_GUIDE_KIND_OF")..LoadStr("AUTO_UPDATE_GUIDE_GAMES");
        ui_utility.setproperty_by_id(dw_gamenum_id, 'Caption', s_gamenum_txt);
    end;

};

update_guide_tips_view.init();

